package easy;

public class Solution_852 {

    /*public int peakIndexInMountainArray(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < arr[i - 1]) {
                return i - 1;
            }
        }
        return 0;
    }*/

    public int peakIndexInMountainArray(int[] arr) {
        for (int i = 0, j = arr.length; i < j; ) {
            if (i + 1 == j) {
                if (arr[i] < arr[j]) {
                    return j;
                } else {
                    return i;
                }
            }
            int m = (i + j) / 2;
            if (arr[m] > arr[m - 1]) {
                i = m;
            } else {
                j = m;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        Solution_852 model = new Solution_852();
        System.out.println(model.peakIndexInMountainArray(new int[]{0, 1, 0}));
        System.out.println(model.peakIndexInMountainArray(new int[]{0, 2, 1, 0}));
        System.out.println(model.peakIndexInMountainArray(new int[]{0, 10, 5, 2}));
        System.out.println(model.peakIndexInMountainArray(new int[]{3, 4, 5, 1}));
        System.out.println(model.peakIndexInMountainArray(new int[]{24, 69, 100, 99, 79, 78, 67, 36, 26, 19}));
    }
}
